<template>
<Form class="xtx-form" :validation-schema="mySchema" v-slot="{ errors }" ref="formCom">
    <div class="user-info">
      <img :src='avatar' />
      <p>Hi,{{nickname}} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field class="input" type="text" placeholder="绑定的手机号" v-model="form.mobile" name="mobile" :class="{err:errors.mobile}"/>
      </div>
      <div class="error" v-if="errors.mobile">{{errors.mobile}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field class="input" type="text" placeholder="短信验证码" v-model="form.code" name="code" :class="{err:errors.code}"/>
        <span class="code" @click="getIdentiFying()">{{timer===0? '发送验证码':`${timer}秒后重试`}}</span>
      </div>
      <div class="error" v-if="errors.code">{{errors.code}}</div>
    </div>
    <a href="javascript:;" class="submit" @click="submit()">立即绑定</a>
  </Form>
</template>

<script>
// import qc from "QC"
import { reactive, ref } from 'vue'
import { Field, Form } from 'vee-validate'
import schema from '@/utils/vee-validate-schema'
import { userMobileLogin, mobileQQLogin } from '@/api/user'
import { useIntervalFn } from '@vueuse/core'
import Message from '@/components/library/message'
import { useRouter, useRoute } from 'vue-router'
export default {
  name: 'CallbackBind',
  components: { Form, Field },
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const avatar = ref('null')
    const nickname = ref('null')
    const formCom = ref('null')
    const router = (useRouter())
    const route = (useRoute())

    if (QC.Login.check()) {
      QC.api('get_user_info').success(data => {
      // const {figureurl,nickname}=data.data
      // console.log(nickname,1111);
        avatar.value = data.data.figureurl
        nickname.value = data.data.nickname
      })
    }
    const form = reactive({
      mobile: null,
      code: null
    })
    // 校验规则
    const mySchema = reactive({
      mobile: schema.mobile,
      code: schema.code
    })
    // pause暂停，resume开始
    const timer = ref(0)
    const { pause, resume } = useIntervalFn(() => {
      console.log(123435)
      timer.value--
      if (timer.value === 0) {
        pause()
      }
    }, 1000, false)
    // 点击发送验证码
    const getIdentiFying = () => {
      // 验证手机号格式
      const valid = mySchema.mobile(form.mobile)
      console.log(valid)
      if (valid === true) {
        console.log(timer)
        if (timer.value === 0) {
          console.log('验证码发送成功')
          const { mobile } = form
          userMobileLogin(mobile).then(data => {
            console.log(data.result)
            // 开始计时
            timer.value = 100
            resume()
            Message({ type: 'success', text: '验证码发送成功' })
          }).catch(e => {
            Message({ type: 'error', text: e.response.data.message })
          })
        }
        console.log(111)
      } else {
        // 使用vee库的错误函数显示错误信息，setFieldError(字段，错误信息)
        formCom.value.setFieldError('mobile', valid)
      }
      // console.log(valid)
      // console.log('验证码发送成功')
      // const { mobile } = form
      // identiFying({ mobile }).then(data => {
      //   console.log(data.result)
      // })
    }
    // 点击提交
    const submit = () => {
      mobileQQLogin({ unionId: props.unionId, ...form }).then(data => {
        console.log(111111)
        const { id, avatar, nickname, account, mobile, token } = data.result
        store.commit('user/setUser', { id, avatar, nickname, account, mobile, token })
        console.log(route.query.redirectUrl)
        router.push(store.state.user.redirectUrl)
        Message({ type: 'success', text: '登录成功' })
      }).catch(e => {
        if (e.response.data) {
          Message({ type: 'error', text: e.response.data.message })
        }
      })
    }
    return { nickname, avatar, form, mySchema, getIdentiFying, timer, formCom, submit }
  }
}
</script>

<style scoped lang='less'>
.user-info {
    width: 320px;
    height: 70px;
    margin: 0 auto;
    display: flex;
    background: #f2f2f2;
    align-items: center;
    padding: 0 10px;
    margin-bottom: 25px;
    img {
      background: #f2f2f2;
      width: 50px;
      height: 50px;
    }
    p {
      padding-left: 10px;
    }
  }
  .code {
    position: absolute;
    right: 0;
    top: 0;
    line-height: 50px;
    width: 80px;
    color: #999;
    &:hover {
      cursor: pointer;
    }
  }
</style>
